package com.cskaoyan.wx.controller;

import com.cskaoyan.bo.AuthloginBo;
import com.cskaoyan.shiro.CustomLoginToken;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.bean.bo.WxRegCaptchaBo;
import com.cskaoyan.wx.bean.bo.WxRegisterBo;
import com.cskaoyan.wx.bean.bo.WxResetBo;
import com.cskaoyan.wx.bean.vo.WxLoginVo;
import com.cskaoyan.wx.service.WxAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Collection;

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    String mobile;
    String code;

    @Autowired
    WxAuthService wxAuthService;

    //认证失败,跳转的该页面
    @RequestMapping("unAuthc")
    public String unAuthc(){
        return "认证失败,你不能访问以下资源";
    }

    /**
     * 功能:登录
     * 作者:whj
     */
    @RequestMapping("login")
    public BaseRespVo login(@RequestBody AuthloginBo authloginBo) {
        CustomLoginToken wxToken = new CustomLoginToken(authloginBo.getUsername(), authloginBo.getPassword(), "wx");
        Subject subject = SecurityUtils.getSubject();
        subject.login(wxToken);
        WxLoginVo wxLoginVo = new WxLoginVo();
        try {
            subject.login(wxToken);
        } catch (AuthenticationException e) {
            return BaseRespVo.error("账号或密码不匹配,请重新登录");
        }
        wxLoginVo.setUserInfo(wxAuthService.login(authloginBo));
        Serializable id = subject.getSession().getId();
        Long timeout = subject.getSession().getTimeout();
        wxLoginVo.setTokenExpire(timeout.toString());
        wxLoginVo.setToken(id.toString());
        return BaseRespVo.ok(wxLoginVo);
    }

    /**
     * 功能:登出
     * 作者:whj
     */
    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        //退出时清除session内容
        Session session = subject.getSession();
        Collection<Object> attributeKeys = session.getAttributeKeys();
        session.removeAttribute(attributeKeys);
        return BaseRespVo.ok();
    }

    /**
     * 作者：grz
     * 功能：注册页面手机验证码获取
     */
    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody WxRegCaptchaBo wxRegCaptchaBo) {
        String mobile = wxRegCaptchaBo.getMobile();
        //向该手机号发送验证码
        this.mobile = mobile;
        code = mobile.substring(7, 11);
        return BaseRespVo.ok();
    }


    /**
     * 作者：grz
     * 功能：前台注册用户
     * 404 未知错误
     * 300 验证码错误
     * 400 用户名重复
     * 200 成功
     */
    @RequestMapping("register")
    public BaseRespVo regCaptcha(@RequestBody WxRegisterBo wxRegisterBo) {

        int i = wxAuthService.register(wxRegisterBo, mobile, code);

        if (i == 404) {
            return BaseRespVo.error("未知错误，注册失败");
        } else if (i == 100) {
            return BaseRespVo.error("请先获取验证码");
        } else if (i == 300) {
            return BaseRespVo.error("验证码错误");
        } else if (i == 400) {
            return BaseRespVo.error("该用户名或手机已被注册");
        }
        mobile = null;
        code = null;
        return BaseRespVo.ok("注册成功");
    }


    /**
     * 作者：grz
     * 功能：重置密码
     * 404未知错误
     * 400该手机号码未注册
     * 300验证码错误
     * 100未获取验证码
     * 200成功
     */
    @RequestMapping("reset")
    public BaseRespVo regCaptcha(@RequestBody WxResetBo wxResetBo) {
        int i = wxAuthService.reset(wxResetBo,mobile,code);

        if (i == 404) {
            return BaseRespVo.error("未知错误，重置密码失败");
        } else if (i == 100) {
            return BaseRespVo.error("请先获取验证码");
        } else if (i == 300) {
            return BaseRespVo.error("验证码错误");
        } else if (i == 400) {
            return BaseRespVo.error("该手机号尚未注册");
        }
        mobile = null;
        code = null;
        return BaseRespVo.ok("重置密码成功");

    }


}
